package com.neusoft.tijiancms.service.impl;

import com.neusoft.tijiancms.dto.OrdersPageRequestDTO;
import com.neusoft.tijiancms.dto.OrdersPageResponseDTO;
import com.neusoft.tijiancms.mapper.OrdersMapper;
import com.neusoft.tijiancms.po.Orders;
import com.neusoft.tijiancms.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;


    @Override
    public OrdersPageResponseDTO listOrders(OrdersPageRequestDTO ordersPageRequestDTO) {
        OrdersPageResponseDTO ordersPageResponseDTO=new OrdersPageResponseDTO();
        //    1.计算总行数
        int totalRow=ordersMapper.getOrdersCount(ordersPageRequestDTO);
        //如果总行数为0那么之间返回
        ordersPageResponseDTO.setTotalRow(totalRow);
        if (totalRow==0){
            return ordersPageResponseDTO;
        }

        //    2.计算总页数
        int totalPageNum=0;
        if (totalRow%ordersPageRequestDTO.getMaxPageNum()==0){
             totalPageNum=totalRow/ordersPageRequestDTO.getMaxPageNum();
        }else {
            totalPageNum=totalRow/ordersPageRequestDTO.getMaxPageNum()+1;
        }
        ordersPageResponseDTO.setTotalPageNum(totalPageNum);
        //    3.计算上一页和下一页
        int pageNum=ordersPageRequestDTO.getPageNum();
        if (pageNum>1){
            ordersPageResponseDTO.setPreNum(pageNum-1);
        }
        if (pageNum<totalPageNum){
            ordersPageResponseDTO.setNextNum(pageNum+1);
        }
        //    4.计算查询记录数
        ordersPageRequestDTO.setBeginNum((pageNum-1)*ordersPageRequestDTO.getMaxPageNum());
        //    5.查询分页数据
      List<Orders>list= ordersMapper.listOrders(ordersPageRequestDTO);
       //给返回值填充余下的数据
        ordersPageResponseDTO.setPageNum(pageNum);
        ordersPageResponseDTO.setMaxPageNum(ordersPageRequestDTO.getMaxPageNum());
        ordersPageResponseDTO.setList(list);

        return ordersPageResponseDTO;
    }

    @Override
    public int getOrdersCount(OrdersPageRequestDTO ordersPageRequestDTO) {
        return ordersMapper.getOrdersCount(ordersPageRequestDTO);
    }

    @Override
    public Orders getOrderById(Orders orders) {
        return ordersMapper.getOrderById(orders);
    }

    @Override
    public int updateOrdersState(Orders orders) {
        return ordersMapper.updateOrdersState(orders);
    }
}
